package com.jlboot.core.util;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.model.LoginUser;

import java.util.List;

/**
 * Created by 51594 on 2022/7/28.
 */
public class LoginUtil{

    public static String getLoginType(){
        return StpUtil.getLoginType();
    }

    public static void login(String loginId, LoginUser loginUser){
        // 会话登录，参数填登录人的账号id
        StpUtil.login(loginId);
        SaSession sa=StpUtil.getTokenSession();
        sa.set(Consts.login_user,loginUser);
    }

    public static void logout(){
        StpUtil.logout();
        StpUtil.getTokenSession().clear();
    }

    public static String getUserId(){
        SaSession sa=StpUtil.getTokenSession();
        LoginUser LoginUser=(LoginUser)sa.get(Consts.login_user);
        if(LoginUser!=null){
            return LoginUser.getUserId();
        }
        return null;
    }

    public static String getUserName(){
        SaSession sa=StpUtil.getTokenSession();
        LoginUser LoginUser=(LoginUser)sa.get(Consts.login_user);
        if(LoginUser!=null){
            return LoginUser.getUserName();
        }
        return null;
    }


    public static LoginUser getLoginUser(){
        SaSession sa=StpUtil.getTokenSession();
        return (LoginUser)sa.get(Consts.login_user);
    }


    public static List<String> getRoleList(){
        StpUtil.checkLogin();
        return StpUtil.getRoleList();
    }

    public static List<String> getPermissionList(){
        StpUtil.checkLogin();
        return StpUtil.getPermissionList();
    }

    public static String getLoginId(){
        return (String)StpUtil.getLoginId();
    }

    public static String getTokenSessId(){
        return StpUtil.getTokenSession().getId();
    }

    public static void setTokenSessionValue(String key,Object value){
        StpUtil.getTokenSession().set(key,value);
    }

    public static Object getTokenSessionValue(String key){
        return StpUtil.getTokenSession().get(key);
    }

    public static void setAnonTokenSessionValue(String key,Object value){
        SaSession saSession=StpUtil.getAnonTokenSession();
        saSession.set(key,value);
    }

    public static Object getAnonTokenSessionValue(String key){
        SaSession saSession=StpUtil.getAnonTokenSession();
        return saSession.get(key);
    }

    /**
     * 查看是否登录，如果没有登录，不会抛出异常
     * @return
     */
    public static boolean isLogin(){
        return StpUtil.isLogin();
    }

    /**
     * 检验当前会话是否已经登录, 如果未登录，则抛出异常：`NotLoginException`
     */
    public static void checkLogin(){
        StpUtil.checkLogin();
    }

    /**
     * 任意一个角色满足即可
     * @param roleCodes
     * @return
     */
    public static boolean hasAnyRole(String... roleCodes){
        for (String roleCode : roleCodes) {
            if(StpUtil.hasRole(roleCode)){
                return true;
            }
        }
        return false;
    }

    /**
     * 任意一个权限
     * @param permissionCodes
     * @return
     */
    public static boolean hasAnyPermission(String... permissionCodes){
        for (String permissionCode : permissionCodes) {
            if(StpUtil.hasPermission(permissionCode)){
                return true;
            }
        }
        return false;
    }

}
